/*
 前端和php端的异步接口文件
 规定前端和php端的数据格式，
 */

var Interface;
Interface = {
    reData:{//返回格式模板
        status:0, //状态，1.表示成功,0，未知错误，其他详见各接口函数
        msg:""//提示信息
    },
    ajaxPost:function (asynData, actionUrl, callBack, settings) {
        /*
         异步post
         asynData：提交到服务的数据
         actionUrl：服务地址
         callBack：请求成功后的回调函数，参数包含：1.服务返回的数据;2.settings中的callBackParams
         settings:配置信息，详见下
         */
        var $this = this;//ajax 会改变this指针，所以要缓存
        settings = $.extend(true, {
            //异步请求的等待信息
            "callBackParams":null//调用函数需要传递给回调函数的参数
        }, settings);
        $.ajax({
            type:"POST",
            url:actionUrl,
            timeout:30000,
            data:asynData,
            success:function (data) {//将数据原样返回
                try {//未登录跳转到登录界面
                    var reData = $.parseJSON(data);
                    if(reData.status==-1){
                        callBack(data);
                        return false;
                    }
                } catch (err) {
                }

                if (settings.callBackParams) {
                    callBack(data, settings.callBackParams);
                } else {
                    callBack(data);
                }
            }, //end success
            error:function (jqXHR, textStatus, errorThrown) {
                //$this.showMsg("异步加载失败");
                var data = {
                    'status':0,
                    'msg':'加载失败'
                }
                callBack(data);
            }//end error
        });//end ajax
    },
    //user
    loginUrl:"/user/loginajax",//异步登录地址
    login:function(account,pwd,type,id,op,bid,url,key,btype,callback,settings){//用户登录
        /*
         account：账户
         pwd:密码
         */
        var data={"username":account,"password":pwd,"r_type":type,"r_value":"id="+id+"&op="+op+"&bid="+bid+"&url="+url+"&k="+key+"&type="+btype};
        this.ajaxPost(data,this.loginUrl,callback,settings);
    },
    

    checkPwdUrl:"/user/checkPwdAjax",
    checkPwd:function (pwd, callback, settings) {//验证密码
        var data = {"pwd":pwd};
        this.ajaxPost(data, this.checkPwdUrl, callback, settings);
    },

    checkUsernameUrl:"/user/CheckUsernameAjax",
    checkUsername:function (username, callback, settings) {
        var data = {"username":username};
        this.ajaxPost(data, this.checkUsernameUrl, callback, settings);
    },

    followBlogUrl:"/follow/followAjax", //关注博客
    followBlog:function (blogId, keyWord, titleLength, callback, settings) {
        var data = {"blogId":blogId, 'keyWord':keyWord, "titleLength":titleLength};
        this.ajaxPost(data, this.followBlogUrl, callback, settings);
    },

    followBlogOffUrl:"/follow/unFollowAjax", //取消博客关注
    followBlogOff:function (blogId, callback, settings) {
        var data = {"blogId":blogId};
        this.ajaxPost(data, this.followBlogOffUrl, callback, settings);
    },

    followBoardUrl:"/board/FollowAjax", //关注图集
    followBoard:function (boardId, callback, settings) {
        var data = {"boardId":boardId};
        this.ajaxPost(data, this.followBoardUrl, callback, settings);
    },

    followBoardOffUrl:"/board/UnfollowAjax", //取消图集关注
    followBoardOff:function (boardId, callback, settings) {
        var data = {"boardId":boardId};
        this.ajaxPost(data, this.followBoardOffUrl, callback, settings);
    },
    getBoardUrl:"/board/GetAjax", //获取登录用户的图集
    getBoard:function (callback, settings) {
        this.ajaxPost(null, this.getBoardUrl, callback, settings);
    },
    getAllBoardUrl:"/board/GetBoardAjax", //获取指定类型的图集
    getAllBoard:function (type, page, lastIndex, callback, settings) {
        var data = {type:type, page:page, "next_cursor":lastIndex};
        this.ajaxPost(data, this.getAllBoardUrl, callback, settings);
    },

    createBoradUrl:"/board/AddAjax", //创建图集
    createBoard:function (boardName, categrayId, type, boardDescription, callback, settings) {
        var data = {"boardName":boardName, "categrayId":categrayId, "boardDescription":boardDescription, 'type':type};
        this.ajaxPost(data, this.createBoradUrl, callback, settings);
    },

    likeUrl:"/collect/LikeAjax", //赞文章
    like:function (data, callback, settings) {
        this.ajaxPost(data, this.likeUrl, callback, settings);
    },

    likeOffUrl:"/collect/UnLikeAjax", //赞文章
    likeOff:function (data, callback, settings) {
        this.ajaxPost(data, this.likeOffUrl, callback, settings);
    },

    commentUrl:"/note/replyAjax", //评论文章
    comment:function (postId, toBlogId, reply, callback, settings) {
        var data = {"postId":postId, "toBlogId":toBlogId, "content":reply};
        this.ajaxPost(data, this.commentUrl, callback, settings);
    },

    reCollectUrl:"/collect/ReCollectAjax", //转采
    reCollect:function (data, callback, settings) {
        this.ajaxPost(data, this.reCollectUrl, callback, settings);
    },
    uploadUrl:'/collect/uploadPost', //发布图片文章地址
    pubPicture:function (boardId, description, photos, sync,type,callback, settings) {//发布图片文章
        var data = {'boardId':boardId, 'description':description, 'photos':photos, 'sync':sync,'type':type};
        this.ajaxPost(data, this.uploadUrl, callback, settings);
    },
    delPostUrl:"/collect/DeletePostAjax", //删除文章
    delPost:function (postId,type, callback, settings) {//type['public','private']
        var data = {postId:postId,type:type};
        this.ajaxPost(data, this.delPostUrl, callback, settings);
    },

    getPostsUrl:"/collect/GetPostAjax", //获取文章
    getPosts:function (categrayId, page, postType, lastIndex, blogUrl, callback, settings) {
        var data = {"categrayId":categrayId, "page":page, "sort":postType, "next_cursor":lastIndex, "url":blogUrl};
        this.ajaxPost(data, this.getPostsUrl, callback, settings);
    },

    getBlogBoardUrl:"/board/GetBlogBoardAjax", //取博主的图集
    getBlogBoard:function (blogId, page, lastIndex, callback, settings) {
        var data = {blogId:blogId, page:page, "next_cursor":lastIndex};
        this.ajaxPost(data, this.getBlogBoardUrl, callback, settings);
    },

    getBlogBoard_settingUrl:"/board/GetBlogBoard_SettingAjax", //取博主的图集(个人中心)
    getBlogBoard_setting:function (blogId, page, callback, settings) {
        var data = {blogId:blogId, page:page};
        this.ajaxPost(data, this.getBlogBoard_settingUrl, callback, settings);
    },

    checkBlogUrlUrl:"/blog/checkBlogUrlAjax", //检测博客地址（注册）
    checkBlogUrl:function (blogUrl, callback, settings) {
        var data = {"blogUrl":blogUrl};
        this.ajaxPost(data, this.checkBlogUrlUrl, callback, settings);
    },
    getBlogActionUrl:"/pinboard/GetBlogInteractionNotesAjax",
    getBlogAction:function (blogId, page, callback, settings) {
        var data = {"blogId":blogId, page:page};
        this.ajaxPost(data, this.getBlogActionUrl, callback, settings);
    },

    //取详情页的同采自接口
    getSameFromUrl:'/pinboard/GetSameSourceAjax',
    getSameFrom:function(postId,callback,settings){
        var data={'postId':postId};
        this.ajaxPost(data, this.getSameFromUrl, callback, settings);
    },


    tipWindow:function (title, html, options) {//带内容的弹层,html一定要带有html
        if (title == "")
            title = "标题";
        var _base = {"modal":true, "title":title};
        var _options = $.extend(_base, options);

        return new Boxy(html, _options);
    },

    showSucMsg:function (txt, left, top) {
        if (!(left && top)) {
            top = ($(document).height() + $(document).scrollTop()) / 2;
            left = $(document).width() / 2 - 60;
        }
        var $msg = $("<div class='settingMsg_wrapper' style='left:" + left + "px;top:" + top + "px'><div class='settingMsg_ico'></div><div class='settingMsg_txt'>" + txt + "</div></div>");
        $msg.hide().appendTo("body").fadeIn();
        setTimeout(function () {
            $msg.fadeOut();
            setTimeout(function () {
                $msg.remove();
            }, 3000);
        }, 2000);
    },

    boxyConfirm:function (txt, handle) {
        var html='<table class="boxyMsgWrapper">' +
            '<tr><td colspan="2" style=" background-color: #EAEBEE; padding: 5px 5px 0;"><span class="boxy_inner_title">提示</span><a class="boxy_inner_title_close boxy_ico" href="#"></a> </td></tr>'+
            '<tr><td  valign="top" style="padding: 10px 0 10px 50px; width:23px;"> <span class="boxy_ico boxy_alert boxyMsg_ico png24"></span></td><td class="boxyMsg_txt">'+txt+'</td></tr></table>';
        var $boxy=$(html).find(".boxy_inner_title_close").click(function(){
            handle(false);
            $(this).parents(".boxy-wrapper").add(".boxy-modal-blackout").remove();
            return false;
        }).end();
        Boxy.ask($boxy,[["确定","boxy_ico boxy_submit png24"],["取消","boxy_ico boxy_canle png24"]],function(val){handle(val=="确定")},{"autoClose":false,"modal":true});
    },
    caituPOP:null,
    boxyBubble:function (txt, type, callback) {
        var icoClass = "boxyMsg_ico_alert";
        switch (type) {
            case "alert":
                break;
            case "error":
                break;
            case "suc":
                icoClass = "boxyMsg_ico_suc";
        }
        html = '<div><table class="boxyMsgWrapper" style="width:240px"><tr><td  valign="top" style="padding:15px 0 10px 30px; width: 33px;"> <span class="' + icoClass + ' boxyMsg_ico"></span></td><td class="boxyMsg_txt" style="padding:15px 30px 10px 5px">' + txt + '</td></tr></table></div>';

        var callback = typeof callback == 'function' ? callback : function(){};
        this.caituPOP = new Boxy(html, {"autoClose":true,'afterHide':callback});


    },
    boxyAlert:function (txt) {
       // alert(txt);
        Boxy.alert('<div><table class="boxyMsgWrapper"><tr><td  valign="top" style="padding:15px 0 10px 60px;width: 24px;"> <span class="boxy_ico boxy_alert png24"></span></td><td class="boxyMsg_txt" style="padding:15px 60px 10px 0">'+txt+'</td></tr></table></div>',null,{"autoClose":false,"modal":true});
    },


    version:1
};//end interface